
#include "Resizer.h"
#include "GraphicsObject.h"
#include <QPainter>
#include <QGraphicsSceneMouseEvent>
#include <QtGui>
#include "common.h"
#include "Tag.h"


//-------------------------------------

Resizer::Resizer( GraphicsObject * n, int t ) :
    QGraphicsItem()
{
    obj = n;
    UpdatePosition();
    IsDragging = false;
    dot = (DotType)t;

    switch( dot )
    {
        case TL:
        case BR:
            setCursor( Qt::SizeFDiagCursor );
            break;
        case MT:
        case MB:
            setCursor( Qt::SizeVerCursor );
            break;
        case TR:
        case BL:
            setCursor( Qt::SizeBDiagCursor );
            break;
        case ML:
        case MR:
            setCursor( Qt::SizeHorCursor );
            break;
    }

    setFlags( ItemIgnoresTransformations );
}

//-------------------------------------

void Resizer::UpdatePosition()
{
    //Tag * t = dynamic_cast< Tag* >( obj );
    QRectF r( obj->GetRect() );
    int m = dot;
    if( m > 3 )
        m++;
    QPointF p( r.x() + r.width() * qreal( m - (m/3)*3 )/2, r.y() + r.height() * qreal( m/3 ) /2 );
    setPos( p );
    setVisible( true );
    update();
}

//-------------------------------------

void Resizer::paint( QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget )
{
    painter->setPen( clSelectDef );
    painter->setBrush( Qt::NoBrush );
    painter->drawRect( boundingRect() );
}

//-------------------------------------

void Resizer::mousePressEvent( QGraphicsSceneMouseEvent * e )
{
    IsDragging = true;
    //QGraphicsItem::mousePressEvent( e );
}

//-------------------------------------

void Resizer::mouseMoveEvent( QGraphicsSceneMouseEvent * e )
{
    QRectF r = obj->GetRect();
    QPointF pos = mapFromScene( e->scenePos() );

    qreal dx = pos.x();
    qreal dy = pos.y();
    qreal mx1 = 0;
    qreal my1 = 0;
    qreal mx2 = 0;
    qreal my2 = 0;


    switch( dot )
    {
        case TL:
            mx1 = 1;
            my1 = 1;
            break;
        case MT:
            my1 = 1;
            break;
        case TR:
            mx2 = 1;
            my1 = 1;
            break;
        case ML:
            mx1=1;
            break;
        case MR:
            mx2 = 1;
            break;
        case BL:
            mx1 = 1;
            my2 = 1;
            break;
        case BR:
            mx2 = 1;
            my2 = 1;
            break;
        case MB:
            my2 = 1;
            break;
    }
    r.adjust( dx * mx1, dy * my1, dx * mx2, dy * my2 );
    obj->Resize( r );
    moveBy( dx*( mx1 + mx2 ), dy*( my1 + my2 ) );

    //QGraphicsItem::mouseMoveEvent( e );
}

//-------------------------------------

void Resizer::mouseReleaseEvent( QGraphicsSceneMouseEvent * e )
{
    IsDragging = false;
    e->accept();
    //QGraphicsItem::mouseReleaseEvent( e );
}

//-------------------------------------

QRectF Resizer::boundingRect() const
{
    return QRectF( -dotSize/2, -dotSize/2, dotSize, dotSize );
}

//-------------------------------------

